<template>
  <el-form :model="record" :rules="rules" ref="form" label-width="60px" >
    <el-form-item><h2>当前操作:转账。请输入账号和金额后点"转账"按钮</h2></el-form-item>
    <el-form-item label="转入账户" prop="receiveCardNo">
      <el-input type="text" v-model="record.receiveCardNo" ></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="price">
      <el-input type="text" v-model="record.price" ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">转账</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
    export default {
      name: "TransforMoney",
      data(){
        let checkCardLength=(rule,value,callback)=>{
          let check=/^\d{18}$/
          if(!check.test(value)){
            callback(new Error("卡号需要为18位数字！！!"))
          }else{
            callback()
          }
        }
        let checkPrice=(rule,value,callback)=>{
          let check=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
          if(!check.test(value)){
            callback(new Error("金额必须是纯数字，最多小数点后两位小数"))
          }else{
            callback()
          }
        }
          return{
            rules:{
              receiveCardNo:[
                {required:true,message:"转入账户不能为空!!!",trigger:"blur"},
                {validator:checkCardLength,trigger:"blur"}
                ],
              price:[
                {required:true,message:"转入金额不能为空!!!",trigger:"blur"},
                {validator:checkPrice,trigger:"blur"}
                ]
            },
            sendCardNo:this.$route.query.sendCardNo,
            record:{
              receiveCardNo:"",
              price:""
            }
          }
      },
      methods:{
        submitForm(){
          this.$refs["form"].validate((val)=>{
             if(val){
               this.axios({
                 method:"get",
                 url:"/check/account/"+this.record.receiveCardNo
               }).then((res)=>{
                 console.log(res.data)
                 if(res.data==-1){
                   alert("转入账户不存在！！！")
                 }else if(res.data==0){
                   alert("转入账户已被冻结!!!")
                 }else{
                   console.log(this.sendCardNo)
                   console.log(this.record.receiveCardNo)
                   console.log(this.record.price)
                   this.axios({
                     method:"get",
                     url:"/transfor/account",
                     params:{
                       sendCardNo:this.sendCardNo,
                       receiveCardNo:this.record.receiveCardNo,
                       price:this.record.price
                     }
                   }).then((res)=>{
                     this.$router.push("/transforSuccess")
                   })
                 }
               })
             }
          })
        }
      }
    }
</script>

<style scoped>

</style>
